var actbill = require('../../../../xc/model/poster')
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    proid: {
      type: Number,
      value: ''
    },
    acitivityid: {
      type: Number,
      value:0
    },
    activitytype: {
      type: Number,
      value:0
    },
    qr: {
      type: String,
      value: ''
    },
    wechat:{
      type:Boolean,
      value:true
    },
    poster:{
      type:Boolean,
      value:true
    },
    showbtn:{
      type:Boolean,
      value:true
    }
    
  },
  /**
   * 组件的初始数据
   */
  data: {
    showShare: false,
    postershow: false,
    postlist: [],
    actidx:0,
    type:"",    
  },
  ready: function () {
    // console.log("this.data.showbtn")

    //  console.log(this.data.showbtn)
      // this. showposter()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    sharebtn(e){
      this.setData({ showShare: !this.data.showShare })
    },
    showSharefunc() {
      console.log("this.proid")
      console.log(this.proid)
      this.setData({ showShare: !this.data.showShare })
    },
    changposter(ev){
      var dataset= ev.currentTarget.dataset;
      var idx = dataset.idx;
      var update={};
      var that= this;
      update["actidx"]=idx;
        if(!this.data.postlist[idx].url){
          this.loadposter(this.data.postlist[idx].id)       
        }
        that.setData(update)
    },

    loadposter(posterid){
      var para = { id: this.data.proid, op: "pro" };
      if(this.data.acitivityid){
        para['acitivityid']=this.data.acitivityid;
      }
      if(this.data.activitytype){
        para['activitytype']=this.data.activitytype;
      }
      if(posterid){
        para["posterid"]=posterid;
      }
      var qr=this.data.qr;
    
      if(!qr){
        var app= getApp()
        qr= app.xc_sys_config.getshareurl()
      }
      
      para["qr"]=qr
      
      var  that = this;
      actbill.poster(para).then(function (item) {
        if(posterid)
        {
          var keyval='postlist['+ that.data.actidx +'].url'
          var updatemodel={}
          updatemodel[keyval]=item.obj.url  
          that.setData(updatemodel)
        }
        else
        {
          if(item.status==1){          
            var upmodel= {};
            upmodel["type"]=item.obj.type;
            var group=[];
            if( upmodel["type"]=="2"){
              group=item.obj.group
            }
            if( upmodel["type"]=="1"){
              group.push({})         
            }
            group[0]["url"]=item.obj.url        
             upmodel["postlist"]=group           
           that.setData(upmodel)
          }
        }   
              
      })
    },
    //显示海报
    showposter() {
      this.setData({ postershow: !this.data.postershow })     
      if(this.data.postlist.length==0)
      {
        this.loadposter();
      } 
    
    },
    //保存海报
    saveImageToPhotosAlbum() {
      var that = this;
      wx.downloadFile({
        url: that.data.postlist[that.data.actidx ].url,
        success: function (res) {
     
          var tempFilePath = res.tempFilePath;
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: function (res) {
              wx.saveImageToPhotosAlbum({
                filePath: tempFilePath,
                success: function (res) {
                  wx.showToast({
                    title: '操作成功',
                  })
                },
                fail: function (res) {
                  if (res.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
                    wx.openSetting({
                      success(settingdata) { }
                    })
                  }
                }
              })
            }
          })
  
  
        }
      })
  
    }

  }
})
